#include "config.h"
#include <fstream>
#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

Config::Config() {
    // 设置默认值
    includeLetters = true;
    includeNumbers = true;
    includeQuestion = false;
    includeEqual = false;
    includeComma = false;
    includePeriod = false;
    includeSlash = false;
    
    charCount = 5;
    wpm = 15;
    frequency = 800;
    
    // 尝试加载配置
    loadConfig();
}

Config::~Config() {
    // 保存配置
    saveConfig();
}

std::string Config::getCharset() const {
    std::string charset;
    
    if (includeLetters) {
        for (char c = 'A'; c <= 'Z'; ++c) {
            charset += c;
        }
    }
    
    if (includeNumbers) {
        for (char c = '0'; c <= '9'; ++c) {
            charset += c;
        }
    }
    
    if (includeQuestion) charset += '?';
    if (includeEqual) charset += '=';
    if (includeComma) charset += ',';
    if (includePeriod) charset += '.';
    if (includeSlash) charset += '/';
    
    return charset;
}

void Config::loadConfig() {
    std::string configPath = getenv("HOME");
    configPath += "/.cwtrainerX.conf";
    
    std::ifstream file(configPath);
    if (!file.is_open()) {
        std::cout << "Config file not found, using defaults." << std::endl;
        return;
    }
    
    std::string line;
    while (std::getline(file, line)) {
        size_t pos = line.find('=');
        if (pos != std::string::npos) {
            std::string key = line.substr(0, pos);
            std::string value = line.substr(pos + 1);
            
            if (key == "include_letters") includeLetters = (value == "true");
            else if (key == "include_numbers") includeNumbers = (value == "true");
            else if (key == "include_question") includeQuestion = (value == "true");
            else if (key == "include_equal") includeEqual = (value == "true");
            else if (key == "include_comma") includeComma = (value == "true");
            else if (key == "include_period") includePeriod = (value == "true");
            else if (key == "include_slash") includeSlash = (value == "true");
            else if (key == "char_count") charCount = std::stoi(value);
            else if (key == "wpm") wpm = std::stoi(value);
            else if (key == "frequency") frequency = std::stoi(value);
        }
    }
    
    file.close();
}

void Config::saveConfig() {
    std::string configPath = getenv("HOME");
    configPath += "/.cwtrainerX.conf";
    
    // 确保目录存在
    fs::path dir = fs::path(configPath).parent_path();
    if (!fs::exists(dir)) {
        fs::create_directories(dir);
    }
    
    std::ofstream file(configPath);
    if (!file.is_open()) {
        std::cerr << "Failed to save config file." << std::endl;
        return;
    }
    
    file << "include_letters=" << (includeLetters ? "true" : "false") << "\n";
    file << "include_numbers=" << (includeNumbers ? "true" : "false") << "\n";
    file << "include_question=" << (includeQuestion ? "true" : "false") << "\n";
    file << "include_equal=" << (includeEqual ? "true" : "false") << "\n";
    file << "include_comma=" << (includeComma ? "true" : "false") << "\n";
    file << "include_period=" << (includePeriod ? "true" : "false") << "\n";
    file << "include_slash=" << (includeSlash ? "true" : "false") << "\n";
    file << "char_count=" << charCount << "\n";
    file << "wpm=" << wpm << "\n";
    file << "frequency=" << frequency << "\n";
    
    file.close();
}